package edu.thu.studio.ormdesigner.editor.editpart;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;

import edu.thu.studio.ormdesigner.model.AbstractModel;
import edu.thu.studio.ormdesigner.util.Utils;

public abstract class AbstractModelEditPart extends AbstractGraphicalEditPart implements PropertyChangeListener {
    
    private static Logger logger = Logger.getLogger(AbstractModelEditPart.class.getName());
    
    private static final boolean DEBUG = false;
    
    @Override
    public void activate() {
        super.activate();
        
        AbstractModel model = (AbstractModel) this.getModel();
        model.addPropertyChangeListener(this);
    }
    
    @Override
    public void deactivate() {
        AbstractModel model = (AbstractModel) this.getModel();
        model.removePropertyChangeListener(this);
        
        super.deactivate();
    }
    
    protected void executeCommand(Command command) {
        this.getViewer().getEditDomain().getCommandStack().execute(command);
    }
    
    public final void propertyChange(PropertyChangeEvent event) {
        try {
            if (DEBUG) {
                logger.log(Level.INFO,
                    this.getClass().getName() + ":" + event.getPropertyName() + ":" + event.toString());
            }
            this.doPropertyChange(event);
        } catch (Exception e) {
            Utils.showExceptionDialog(e);
        }
    }
    
    protected void doPropertyChange(PropertyChangeEvent event) {
        if (event.getPropertyName().equals("refreshVisuals")) {
            this.refreshVisuals();
        } else if (event.getPropertyName().equals("refresh")) {
            this.refresh();
        }
    }
    
    @Override
    public void refresh() {
        refreshChildren();
        refreshVisuals();
        
        refreshSourceConnections();
        refreshTargetConnections();
    }
    
}
